package com.ruoyi.web.controller.drug;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.drug.domain.DrugsOrderDetail;
import com.ruoyi.drug.service.IDrugsOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单明细Controller
 *
 * @author ruoyi
 * @date 2022-11-14
 */
@RestController
@RequestMapping("/drug/detail")
public class DrugsOrderDetailController extends BaseController
{
    @Autowired
    private IDrugsOrderDetailService drugsOrderDetailService;

    /**
     * 查询订单明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:detail:list')")
    @GetMapping("/list")
    public TableDataInfo list(DrugsOrderDetail drugsOrderDetail)
    {
        startPage();
        List<DrugsOrderDetail> list = drugsOrderDetailService.selectDrugsOrderDetailList(drugsOrderDetail);
        return getDataTable(list);
    }

    /**
     * 导出订单明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:detail:export')")
    @Log(title = "订单明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DrugsOrderDetail drugsOrderDetail)
    {
        List<DrugsOrderDetail> list = drugsOrderDetailService.selectDrugsOrderDetailList(drugsOrderDetail);
        ExcelUtil<DrugsOrderDetail> util = new ExcelUtil<DrugsOrderDetail>(DrugsOrderDetail.class);
        util.exportExcel(response, list, "订单明细数据");
    }

    /**
     * 获取订单明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:detail:query')")
    @GetMapping(value = "/{detailId}")
    public AjaxResult getInfo(@PathVariable("detailId") Long detailId)
    {
        return success(drugsOrderDetailService.selectDrugsOrderDetailByDetailId(detailId));
    }

    /**
     * 新增订单明细
     */
    @PreAuthorize("@ss.hasPermi('system:detail:add')")
    @Log(title = "订单明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DrugsOrderDetail drugsOrderDetail)
    {
        return toAjax(drugsOrderDetailService.insertDrugsOrderDetail(drugsOrderDetail));
    }

    /**
     * 新增订单明细
     */
    @PreAuthorize("@ss.hasPermi('system:detail:add')")
    @Log(title = "订单明细", businessType = BusinessType.INSERT)
    @GetMapping(value = "/{drugIds}/{orderId}")
    public AjaxResult addDrugOrder(@PathVariable("drugIds") Long[] drugIds,@PathVariable("orderId") Long orderId)
    {
        drugsOrderDetailService.insertDrugsOrderDetails(drugIds,orderId);
        return toAjax(true);
    }

    /**
     * 修改订单明细
     */
    @PreAuthorize("@ss.hasPermi('system:detail:edit')")
    @Log(title = "订单明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DrugsOrderDetail drugsOrderDetail)
    {
        return toAjax(drugsOrderDetailService.updateDrugsOrderDetail(drugsOrderDetail));
    }

    /**
     * 删除订单明细
     */
    @PreAuthorize("@ss.hasPermi('system:detail:remove')")
    @Log(title = "订单明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{detailIds}")
    public AjaxResult remove(@PathVariable Long[] detailIds)
    {
        return toAjax(drugsOrderDetailService.deleteDrugsOrderDetailByDetailIds(detailIds));
    }
}
